//
//  WZhuSeBan.m
//  W2MD
//
//  Created by zhudongwei on 14/10/29.
//  Copyright (c) 2014年 zhudongwei. All rights reserved.
//

#import "WZhuSeBan.h"

@implementation WZhuSeBan

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (UIColor *) colorWithHexString: (NSString *)color
{
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
        return [UIColor clearColor];
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    
    //r
    NSString *rString = [cString substringWithRange:range];
    
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
}

-(void)showYanse:(NSArray*)yanseArr
{
    for (int i = 210; i < 240; i++) {
        if ([self.colorTypeView viewWithTag:i]) {
            [[self.colorTypeView viewWithTag:i] removeFromSuperview];
        }
    }
    if ([yanseArr count]>0) {
        for (int i =0; i<[yanseArr count]; i++) {
            NSMutableDictionary *valueDic = [yanseArr objectAtIndex:i];
            //UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(2+30*i, 2, 25, 25)];
            UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(self.colorTypeView.frame.size.width-32*(i+1), 2, 25, 25)];
            [iv setImage:[UIImage imageNamed:[valueDic objectForKey:@"Icon"]]];
            iv.tag = 210+i;
            [self.colorTypeView addSubview:iv];
        }
    }
}

-(void)showPeise:(NSArray*)peiseArr
{
    self.secColorLbl.text = @"";
    if ([peiseArr count]>0) {
        NSMutableString *text = [[NSMutableString alloc] init];
        for (int i =0; i <[peiseArr count]; i++) {
            NSMutableDictionary *valueDic = [peiseArr objectAtIndex:i];
            NSString *wenziValue = [valueDic objectForKey:@"desc"];
            NSString *d1Value = [wenziValue substringWithRange:NSMakeRange(0, 1)];
            [text appendString:d1Value];
            [text appendString:@" "];
        }
        
        self.secColorLbl.text = text;
        self.secColorLbl.textAlignment = NSTextAlignmentRight;
    }
}

-(void)showDesc:(NSMutableDictionary*)dicinfo
{
    NSMutableDictionary *peiseDic = [dicinfo objectForKey:@"zhuse"];
    NSArray *peiseArr = [peiseDic allValues];
    [self showPeise:peiseArr];
    
    NSMutableDictionary *yanseDic = [dicinfo objectForKey:@"yanse"];
    NSArray *yanseArray = [yanseDic allValues];
    [self showYanse:yanseArray];

}

@end
